<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" class="search-component">
      <el-form-item label="菜单标题">
        <el-input
          v-model="searchCondition.keyWord"
          placeholder="请输入菜单标题"
          clearable
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="searchCondition.createTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        />
      </el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="medium"
        @click="initMenuTable"
      >搜索</el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh"
        size="medium"
        @click="resetSearchCondition"
      >重置</el-button>
    </el-form>
    <!-- 操作按钮 -->
    <el-form :inline="true" class="operation-component">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="showAddMenuForm"
      >新&nbsp;&nbsp;&nbsp;&nbsp;增</el-button>

      <el-button
        type="danger"
        icon="el-icon-delete"
        size="mini"
        @click="deleteMenu"
      >批量删除</el-button>
      <el-button
        type="success"
        icon="el-icon-download"
        size="mini"
        @click="exportMenu"
      >批量导出</el-button>
    </el-form>
    <!-- 列表 -->
    <el-table
      ref="menuTable"
      :data="tableData"
      highlight-current-row
      row-key="id"
      style="width: 100%"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="title" label="菜单标题" width="width" />
      <el-table-column prop="permission" label="权限标识" width="width" />
      <el-table-column prop="path" label="菜单路由" width="width" />
      <el-table-column
        prop="hidden==1"
        label="是否隐藏"
        align="center"
        width="width"
      >
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.hidden"
            active-color="#ff4949"
            inactive-color="#13ce66"
            @change="changeHidden(row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="menuSort" label="排序" width="width" sortable />
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="width"
        sortable
      />
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="small"
            @click="showUpdateMenuForm(row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page.current"
      :total="page.total"
      :page-size="page.size"
      style="padding: 20px 0"
      layout="total"
      @current-change="initMenuTable"
      @size-change="handleSizeChange"
    />
    <!-- 新增/修改表单 -->
    <el-dialog
      :title="menu.menuId ? '修改菜单' : '新增菜单'"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form ref="menuForm" :model="menu" :rules="menuRules" :inline="true" label-width="80px">
        <el-form-item label="菜单名" prop="name">
          <el-input v-model="menu.name" />
        </el-form-item>
        <el-form-item label="排序" prop="menuSort">
          <el-input-number v-model="menu.menuSort" :min="1" :max="999" label="请输入排序" />
        </el-form-item>
        <el-form-item label="权限标识" prop="permission">
          <el-input v-model="menu.permission" />
        </el-form-item>
        <el-form-item label="菜单路由" prop="path">
          <el-input v-model="menu.path" />
        </el-form-item>
        <el-form-item label="上级菜单" prop="pid">
          <el-select v-model="menu.pid" placeholder="请选择部门">
            <el-option
              v-for="item in menu.menuList"
              :key="item.menuId"
              :label="item.title"
              :value="item.menu"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否隐藏">
          <el-radio v-model="menu.hidden" label="true">是</el-radio>
          <el-radio v-model="menu.hidden" label="false">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveDialogForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Config from '@/settings'
export default {
  name: 'Menu',
  data() {
    return {
      page: {
        total: 0, // 总记录数
        current: 1, // 当前页码
        size: 9999, // 每页记录数
        orders: [{ column: 'id', asc: true }]
      },
      searchCondition: {
        createTime: [],
        keyWord: ''
      },
      tableData: [], // 列表数据
      dialogFormVisible: false, // 是否展示新增/修改表单
      selectedIds: [], // 所有选择的id的数组
      menu: {
        menuId: undefined,
        name: '',
        menuSort: 1,
        permission: '',
        path: '',
        hidden: false,
        menuList: []
      }, // 当前要操作的menu
      menuRules: {
        name: [
          { required: true, message: '请输入菜单名' },
          { min: 2, message: '长度至少为2个字符' }
        ],
        permission: [{ required: true, message: '请输入操作标识' }],
        path: [{ required: true, message: '请输入菜单路径标识' }]
      }
    }
  },
  mounted() {
    this.initMenuTable()
  },
  methods: {
    async initMenuTable(current = 1) {
      const { page, searchCondition } = this
      if (Number.isInteger(current)) {
        page.current = current
      }
      const result = await this.$API.menu.pageQuery({ searchCondition, page })
      if (result.code === 200) {
        this.tableData = result.data.records
        this.page.current = result.data.current
        this.page.size = result.data.size
        this.page.total = result.data.total
      }
    },
    resetSearchCondition() {
      this.searchCondition.createTime = []
      this.searchCondition.keyWord = ''
      this.initMenuTable()
    },
    async deleteMenu(row) {
      let menuIds = []
      if (row.menuId) {
        menuIds = [row.menuId]
      } else {
        menuIds = this.selectedIds
      }
      if (menuIds.length === 0) {
        this.$message({
          message: '请选择要操作的数据',
          type: 'warning',
          showClose: true,
          duration: Config.duration
        })
        return
      }
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const result = await this.$API.menu.deleteMenu(menuIds)
          if (result.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              showClose: true,
              duration: Config.duration
            })
            this.initMenuTable()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async showAddMenuForm() {
      const result = await this.$API.menu.queryMenu()
      if (result.code === 200) {
        this.menu = result.data
      }
      this.menu.enabled = 'true'
      this.$nextTick(() => this.$refs.menuForm.clearValidate())
      this.dialogFormVisible = true
    },
    async showUpdateMenuForm(row) {
      const result = await this.$API.menu.queryMenu(row.menuId)
      if (result.code === 200) {
        this.menu = result.data
      }
      this.$nextTick(() => this.$refs.menuForm.clearValidate())
      this.dialogFormVisible = true
    },
    saveDialogForm() {
      this.$refs.menuForm.validate(async valid => {
        if (valid) {
          let code
          const { menu } = this
          if (menu.menuId) {
            const result = await this.$API.menu.updateMenu(menu)
            code = result.code
          } else {
            const result = await this.$API.menu.createMenu(menu)
            code = result.code
          }
          if (code === 200) {
            this.dialogFormVisible = false
            this.$message({
              message: '保存成功',
              type: 'success',
              showClose: true,
              duration: Config.duration
            })
          } else {
            this.$message({
              message: '保存失败',
              type: 'error',
              showClose: true,
              duration: Config.duration
            })
          }
          this.initMenuTable()
        }
      })
    },
    /*
        列表选中状态发生改变的监听回调
      */
    handleSelectionChange(selection) {
      this.selectedIds = selection.map((item) => item.menuId)
    },
    /*
      处理pageSize发生改变的监听回调
      */
    handleSizeChange(pageSize) {
      this.page.size = pageSize
      this.initMenuTable()
    },
    async exportMenu() {
      await this.$API.menu.exportMenu(this.selectedIds)
    },
    async changeHidden(row) {
      const result = await this.$API.menu.changeHidden(
        row.id,
        row.hidden
      )
      if (result.code === 200) {
        this.$message({
          message: '设置显隐成功',
          type: 'success',
          showClose: true,
          duration: Config.duration
        })
      }
    }
  }
}
</script>

  <style scoped>
  </style>

